package com.explam.springboot.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.explam.springboot.common.ResultUtil;
import com.explam.springboot.entity.Income;
import com.explam.springboot.entity.Pay;
import com.explam.springboot.entity.User;
import com.explam.springboot.mapper.IncomeMapper;
import com.explam.springboot.service.IIncomeService;
import com.explam.springboot.service.IPayService;
import com.explam.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/app/income")
public class IncomeAppController {

    @Autowired
    private IIncomeService incomeService;

    @Autowired
    private IncomeMapper incomeMapper;

    @Autowired
    private IPayService payService;

    @Autowired
    private IUserService userService;

    /*
     * 根据用户id获取12个月内每个月的总收入
     * */
    @GetMapping("/month/income-total")
    public ResultUtil getMonIncome(@RequestParam Integer id){
        List<Map<String, Object>> list = incomeMapper.getMonthlyIncomeAmount(id);
        return ResultUtil.success("汇总12个月内每个月的总收入",list);
    }

    /*
    * 统计这个月的收入
    * */
    @GetMapping("/current/money-total")
    public ResultUtil getCurrentMonthTotal(@RequestParam(defaultValue = "") Integer id){
        List<Income> list = incomeMapper.currentTotal(id);
        return ResultUtil.success("success", list);
    }

    /*
    * 保存收入信息
    * */
    @PostMapping("/save")
    public ResultUtil saveIncomeInfo(@RequestBody Income income){
        incomeService.saveOrUpdate(income);
        return ResultUtil.success("保存成功");
    }

    /*
    * 首页获取数据
    * */
    @GetMapping("/index")
    public ResultUtil indexShowIncomeAndPay(@RequestParam(defaultValue = "") Integer id){
        // 获取支出数据
        LambdaQueryWrapper<Pay> payWrapper = new LambdaQueryWrapper<>();
        payWrapper.eq(Pay::getUserId, id);
        payWrapper.orderByDesc(Pay::getCreateTime);
        List<Pay> payList = payService.list(payWrapper);
        // 获取收入数据
        LambdaQueryWrapper<Income> incomeWrapper = new LambdaQueryWrapper<>();
        incomeWrapper.eq(Income::getUserId, id);
        incomeWrapper.orderByDesc(Income::getCreateTime);
        List<Income> incomeList = incomeService.list(incomeWrapper);
        // 获取用户数据
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getId, id);
        User user = userService.getOne(userWrapper);
        // 使用map整合数据
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("user", user);
        hashMap.put("income", incomeList);
        hashMap.put("pay", payList);
        // 将map数据进行整合
        ResultUtil resultUtil = new ResultUtil();
        resultUtil.setCode(ResultUtil.SUCCESS_CODE);
        resultUtil.setMessage("success");
        resultUtil.setData(hashMap);
        return resultUtil;
    }
}
